<template>
  <div>
    <transition name="popup-slide-up">
      <div class="ebook-popup-list" v-show="fontFamilyVisible">
        <div class="ebook-popup-title">
          <div class="ebook-popup-title-icon" @click="hide">
            <span class="icon-down2"></span>
          </div>
          <span class="ebook-popup-title-text">{{$t('book.selectFont')}}</span>
        </div>
        <div class="ebook-popup-list-wrapper">
          <div
            class="ebook-popup-item"
            v-for="(item, i) in fontFamilyList"
            @click="setFontFamily(item.font)"
            :key="i"
          >
            <div
              class="ebook-popup-item-text"
              :class="{ selected: isSelected(item) }"
            >
              {{ item.font }}
            </div>
            <div
              class="ebook-popup-item-check"
              :class="{ selected: isSelected(item) }"
            >
              <span class="icon-check"></span>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { ebookMixin } from '@/utils/mixin.js'
import { FONT_FAMILY } from '@/utils/book.js'
import { saveFontFamily } from '@/utils/localStorage.js'
export default {
  mixins: [ebookMixin],
  data() {
    return {
      // web字体
      fontFamilyList: FONT_FAMILY
    }
  },
  methods: {
    //   隐藏web字体设置
    hide() {
      this.setFontFamilyVisible(false)
    },
    setFontFamily(font) {
      this.setDefaultFontFamily(font)
      // 在本地存储中存储字体
      saveFontFamily(this.fileName, font)
      if (font === 'Default') {
      // 默认字体
        this.currentBook.rendition.themes.font('宋体')
      } else {
        this.currentBook.rendition.themes.font(font)
      }
      console.log(font)
    },
    //   点击文字让其高亮
    isSelected(item) {
      return this.defaultFontFamily === item.font
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/style/global';
.ebook-popup-list {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 300;
  width: 100%;
  background: white;
  box-shadow: 0 px2rem(-4) px2rem(6) rgba(0, 0, 0, 0.1);
  .ebook-popup-title {
    position: relative;
    padding: px2rem(15);
    box-sizing: border-box;
    border-bottom: px2rem(1) solid #b8b9bb;
    text-align: center;
    @include center;
    .ebook-popup-title-icon {
      position: absolute;
      left: px2rem(15);
      font-size: px2rem(16);
    }
    .ebook-popup-title-text {
      font-size: px2rem(14);
      font-weight: bold;
    }
  }
  .ebook-popup-list-wrapper {
    .ebook-popup-item {
      display: flex;
      padding: px2rem(20);
      .ebook-popup-item-text {
        flex: 1;
        text-align: left;
        font-size: px2rem(14);
        &.selected {
          color: #346cb9;
          font-weight: bold;
        }
      }
      .ebook-popup-item-check {
        flex: 1;
        text-align: right;
        font-size: px2rem(14);
        font-weight: bold;
        &.selected {
          color: #346cb9;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
